// pages/foodmaterial.js
var util = require('../../utils/util.js');
const AV = require('../../libs/av-weapp-min.js');
var foodmaterials;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    normal: 'http://p06iwel4h.bkt.clouddn.com/down.png',
    red: 'http://p06iwel4h.bkt.clouddn.com/downred.png',
    foodUrl:"http://p06iwel4h.bkt.clouddn.com/dontstarve/foods/",
    isHidden: true,
    currentClass: '肉类',
    currentType: null,

  },

  getMaterials: function (type) {
    console.log("getMaterials:" + type);
    var materials;
    if (type == '肉类') {
      materials = foodmaterials.FoodMeats;
    } else if (type == '蔬菜类') {
      materials = foodmaterials.FoodVegetables;
    } else if (type == '水果类') {
      materials = foodmaterials.FoodFruit;
    } else if (type == '蛋类') {
      materials = foodmaterials.FoodEggs;
    } else {
      materials = foodmaterials.FoodOthers;
    }
    return materials.Foods;
  },

  onItemClick: function (e) {
    console.log("onItemClick");
    console.log(e.detail.type);
    var type = e.detail.type;
    var materials = this.getMaterials(type);
    this.setData({ currentClass: type, materials: materials });
    wx.pageScrollTo({
      scrollTop: 0,
    })
  },

  showDialogForType: function() {
    console.log("showDialogForType");
    var types = new Array('肉类', '蔬菜类', '水果类', '蛋类');
    this.setData({types: types, isHidden : false, currentType : null});
  },

  changeOrder: function (event) {
    console.log('changeOrder');
    var type = event.currentTarget.dataset.type;
    var currentType;
    var health, hunger, sanity;
    var itemsa;
    if(type == this.data.currentType) {
      currentType = null;
      itemsa = this.getMaterials(this.data.currentClass);
    }
    else {
      currentType = type;
      itemsa = util.bubbleSort(this.data.materials, type);
    }
    this.setData({ materials: itemsa, currentType: currentType});

    wx.pageScrollTo({
      scrollTop: 0,
    })
  },

  showDetail: function (event) {
    var name = event.currentTarget.dataset.name;
    var id = event.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../foodmaterialdetail/foodmaterialdetail?Name=' + name
    })
    console.log('跳转到' + name);
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var self = this;
    var query = new AV.Query('gamedata');
    query.get('5aa4ae269f54543d5759cfb6').then(function (todo) {
      var str = todo.get('value');
      console.log(str);
      foodmaterials = JSON.parse(str);
      var foods = foodmaterials.FoodMeats.Foods;
      console.log(foods);
      self.setData({ materials: foods });
    }, function (error) {
      // 异常处理
    });

    // 本地数据
    // var foodmaterialsdata = require('../../data/foodmaterials_data.js');
    // foodmaterials = foodmaterialsdata.foodmaterials;
    // this.setData({ materials: foodmaterials.FoodMeats.Foods});
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})